package org.neptune.web.application;

import org.mybatis.spring.annotation.MapperScan;
import org.neptune.service.config.NeptuneConfig;
import org.neptune.service.websocket.NettyWebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@ComponentScan(basePackages = "org.neptune")
@MapperScan(basePackages = "org.neptune.service.dao")
@EnableElasticsearchRepositories(basePackages = "org.neptune")
@EnableScheduling
public class NeptuneApplication {

	private static final Logger logger = LoggerFactory.getLogger(NeptuneApplication.class);

	@Autowired
	RestTemplateBuilder restTemplateBuilider;

	@Bean
	public RestTemplate restTemplate() {
		// 使用build()方法进行获取
		return restTemplateBuilider.build();
	}

	public static void main(String[] args) {

		/**
		 * Springboot整合Elasticsearch 在项目启动前设置一下的属性，防止报错 解决netty冲突后初始化client时还会抛出异常
		 * java.lang.IllegalStateException: availableProcessors is already set to [8],
		 * rejecting [8]
		 */
		System.setProperty("es.set.netty.runtime.available.processors", "false");

		SpringApplication.run(NeptuneApplication.class, args);
		logger.info("********* neptune app start! *********");
		try{
			logger.info("启动netty websocket server。。。。。");
			new NettyWebSocketServer(NeptuneConfig.websocketPort).start();
			// 阻塞
			// logger.info("启动netty websocket server完成。。。。。");
		} catch (Exception e){
			logger.info("NettyServerError:" + e.getMessage());
		}
	}
}
